﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Nservice.Billing.Messages;
using Nservice.Sales.Events;
using Nservice.Sales.Messages;
using NServiceBus;


namespace Nservice.Sales.Server
{
    public class PlaceOrderHandler : IHandleMessages<PlaceOrder>
    {
        public IBus Bus { get; set; }

        public void Handle(PlaceOrder message)
        {
            //saving to the database and sending a notification to the credit card company
            //error between saving and sending notification will make this whole transaction
            //null and void..and the fault event will be triggered

            Console.WriteLine(@"Product id: {0}, Order id: {1}", message.ProductId, message.OrderId);

            //save to database the order

            Bus.Send(new CreditCharged
            {
                OrderId = message.OrderId
            });

            //publish an event that the order has accepted
            Bus.Publish(new OrderAccepted
            {
                OrderId = message.OrderId,
                CustomerId = message.CustomerId,
                ProductId = message.ProductId
            });

        }


    }
  
}
